import { Component, OnInit, Input } from '@angular/core';
import { MdlSnackbarService } from '@angular-mdl/core';
import { TreeNode } from 'primeng/primeng';
import { ConfigService } from './cfg.service';

@Component({
    selector: 'menus',
    templateUrl: './menus.component.html',
    styleUrls: ['./menus.component.scss'],
    providers: [ConfigService]
})
export class MenusComponent implements OnInit {
    id: number;
    scrollHeight: string = "450px";
    loading = true;
    procActTree: TreeNode[] = [];
    cols: any[];
    scrollableCols: any[];
    partMenus: any[] = [];
    selectedNode: any;
    frozenCols: any[] = [
        { field: 'partType', header: '人员类型', style: { 'width': '80px', 'text-align': 'center' } },
        { field: 'partName', header: '环节参与者', style: { 'width': '80px', 'text-align': 'center' } },
    ];
    @Input()
    bizId: number = 0;

    @Input()
    systemUrl: string = '';

    @Input()
    set taskListId(taskListId: number) {
        if (!taskListId || !this.bizId) return;
        this.id = taskListId;
        //加载菜单组
        this.configService.loadMenuGroups(this.systemUrl).subscribe(menuGrps => {
            this.cols = menuGrps;
            //初始化流程、环节、参与者
            this.configService.getProcActPartTreeByBizId(this.bizId, this.currentProcDefId, this.systemUrl).subscribe(data => {
                this.loading = false;
                this.procActTree = data;
                if (this.procActTree && this.procActTree.length > 0) {
                    this.selectedNode = this.procActTree[0]['children'][0];
                    this.nodeSelect(null);
                }
            });
        });
    }

    get taskListId(): number { return this.id; }

    currentProcDefId: number = 0;
    @Input()
    set procDefId(procDefId: number){
        if(!procDefId)return;
        this.currentProcDefId = procDefId;
        //加载菜单组
        this.configService.loadMenuGroups(this.systemUrl).subscribe(menuGrps => {
            this.cols = menuGrps;
            //初始化流程、环节、参与者
            this.configService.getProcActPartTreeByBizId(this.bizId, this.currentProcDefId, this.systemUrl).subscribe(data => {
                this.loading = false;
                this.procActTree = data;
                if (this.procActTree && this.procActTree.length > 0) {
                    this.selectedNode = this.procActTree[0]['children'][0];
                    this.nodeSelect(null);
                }
            });
        });
    }

    constructor(
        private configService: ConfigService,
        private mdlSnackbarService: MdlSnackbarService) { }
    ngOnInit() {
    }
    /**节点选中事件 */
    nodeSelect($event) {
        //加载当前岗位菜单配置项
        this.configService.loadMenuAuthority(this.taskListId, this.selectedNode.id.split('_')[1], this.systemUrl).subscribe(menuAuth => this.partMenus = menuAuth);
    }

    /**选中所有行 */
    checkAllPart(item) {
        let checkedFlag = $("#menu_" + item.id).find('input:checkbox').prop('checked');
        this.partMenus.forEach(element => element.auths[item.id] = (checkedFlag ? 1 : 0));
    }

    /**保存 */
    saveUpdate(event) {
        if (this.partMenus.length > 0) {
            let auths = [];
            this.partMenus.forEach(e => {
                for (let x in e.auths) {
                    auths.push({ 'partId': e.partId, 'menuTabId': x, 'code': (e.auths[x] ? 1:0) });
                }
            });
            this.configService.saveUpdateMenuAuthority(auths, +this.selectedNode.id.split('_')[1], this.taskListId, this.systemUrl).subscribe(data => this.mdlSnackbarService.showToast('菜单权限保存成功！'));
        }
    }
}
